﻿using System;
using DomainNamespace;
using LoggerNamespace;

namespace PresenterNamespace
{
    public class Presenter
    {
        private IView view;
        private Queue queue;
        public ILogger logger;

        private Presenter() { }

        public Presenter(IView v)
        {
            this.view = v;
            this.view.addField = "";
            this.view.result = "";

            this.queue = new Queue();
            this.logger = null;
        }

        public Presenter(IView v, ILogger l)
        {
            this.view = v;
            this.view.addField = "";
            this.view.result = "";

            this.queue = new Queue();
            this.logger = l;
        }

        public void addElement() 
        {
            if (this.logger != null) this.logger.write(view.addField + " was added to queue");
            queue.addValue(view.addField);
            this.view.addField = "";
            this.view.result = this.getListOfItems();
        }

        private String getListOfItems()
        {
            String retVal = "";
            for (int i = 0; i < queue.length; i++)
            {
                Object obj = queue.readItem(i);
                retVal = retVal + obj.ToString() + "\n";
            }
            return retVal;
        }

        public void getElement()
        {
            if (queue.length > 0)
            {
                Object obj = queue.getValue();
                if (this.logger != null) this.logger.write("Item " + obj.ToString() + "got from queue");
                this.view.result = this.getListOfItems();
            }
            else
            {
                if (this.logger != null) this.logger.write("Queue is empty");
            }
        }

        public String getLogText()
        {
            if (this.logger != null)
            {
                return this.logger.read();
            }
            else
            {
                return "";
            }
        }

        public void showLog()
        {
            if (this.logger != null) view.showLog(this.getLogText());
        }
    }
}
